package com.tl.commonlibrary.ui.web;

import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tl.commonlibrary.R;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.widget.TitleBar;

/**
 * Created by JongSung on 17/1/16.
 */

public class WebViewModel implements View.OnClickListener, WebListener {
    private WebActivity parent;
    private PopupWindow mPopupMenuWindow;
    private WebFragment webFragment;

    public WebViewModel(WebActivity parent) {
        this.parent = parent;
    }

    public void setActionBar() {
        if (parent != null && parent.getModel() != null) {
            if (parent.getTitleBar() != null) {
                setActionBarTitle();
            }
        }
    }

    private void setActionBarTitle() {
        TitleBar titleBar = parent.getTitleBar();
        if (titleBar != null && !TextUtils.isEmpty(parent.getModel().getActionBarTitle())) {
            titleBar.setTitle(parent.getModel().getActionBarTitle());
        }
    }


    public void setWebFragment(WebFragment webFragment) {
        this.webFragment = webFragment;
        this.webFragment.setWebListener(this);
    }

    public WebFragment getWebFragment() {
        return webFragment;
    }


    @Override
    public void onClick(View v) {
    }


    @Override
    public void onReceivedTitle(WebView view, String title) {
        if (parent.getModel() != null && !TextUtils.isEmpty(title) && !WebActivity.LOAD_HTML.equals(parent.getOriginUrl())) {
            parent.getModel().setActionBarTitle(title);
            setActionBarTitle();
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
    }

}

